package com.ft.oa.service.project.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ft.oa.domain.dto.ApiResult;
import com.ft.oa.domain.dto.project.ExecProjectWeeklyDto;
import com.ft.oa.domain.project.ExecProjectWeekly;
import com.ft.oa.enums.ManagerEnum;
import com.ft.oa.mapper.project.ExecProjectWeeklyMapper;
import com.ft.oa.service.project.ExecProjectWeeklyService;
import com.ft.oa.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 项目周报接口impl
 * @author liyansheng
 * @ClassName ExecProjectWeeklyMapper
 * @date 2022-3-2 16:33:56
 */
@Service
public class ExecProjectWeeklyServiceImpl implements ExecProjectWeeklyService {

    @Resource
    private ExecProjectWeeklyMapper execProjectWeeklyMapper;

    /**
     * param execProjectWeekly
     * return ApiResult
     * describe 新增周报
     * @author liyansheng
     * @date 2022-3-2 16:38:12
     */
    @Override
    public ApiResult addOrUpdate(ExecProjectWeekly execProjectWeekly) {
        //如果id不为空则执行修改操作，如果为空则执行新增操作
        if(execProjectWeekly.getId()!=null){
            UpdateWrapper<ExecProjectWeekly> updateWrapper = new UpdateWrapper();
            updateWrapper.lambda().eq(ExecProjectWeekly::getId,execProjectWeekly.getId())
                    .set(ExecProjectWeekly::getThisWeekSummary,execProjectWeekly.getThisWeekSummary())
                    .set(ExecProjectWeekly::getNextWeekPlan,execProjectWeekly.getNextWeekPlan()
                    );
            execProjectWeeklyMapper.update(null,updateWrapper);
        }else{
            execProjectWeeklyMapper.insert(execProjectWeekly);
        }
        return ApiResult.simpleSuccess();
    }

    /**
     * 查询周报
     * @param execProjectWeeklyDto
     * @return
     */
    @Override
    public ApiResult search(ExecProjectWeeklyDto execProjectWeeklyDto) {
        QueryWrapper<ExecProjectWeekly> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ExecProjectWeekly::getProjectNumber,execProjectWeeklyDto.getProjectNumber())
                .eq(ManagerEnum.GENERAL.toString().equals(SecurityUtils.getCurrentUser().getManager()), ExecProjectWeekly::getCreator,SecurityUtils.getCurrentUser().getId())
                .eq(ExecProjectWeekly::getYearmonth,execProjectWeeklyDto.getYearmonth());//查询非审批驳回的
        List<ExecProjectWeekly> execProjectWeeklies = execProjectWeeklyMapper.selectList(wrapper);
        return ApiResult.successWithData(execProjectWeeklies);
    }
}
